package faf.business.tests;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import javax.persistence.EntityManager;

import junit.framework.Assert;

import org.junit.BeforeClass;
import org.junit.Test;

import faf.business.QuestionManager;
import faf.dao.JpaUtils;
import faf.entities.Answer;
import faf.entities.Properties;
import faf.entities.Question;
import faf.entities.QuestionType;

public class QuestionManagerTests {

	static JpaUtils jpautils;
	static QuestionManager manager;

	@BeforeClass
	public static void beforeClass() {
		HashMap<String, String> config = new HashMap<String, String>();
		config.put("hibernate.hbm2ddl.auto", "create-drop");
		jpautils = new JpaUtils(config);
		manager = new QuestionManager();
		manager.setJpautils(jpautils);
	}

	@Test
	public void testCreateQuestion() {
		Question question = new Question();
		question.setObligatory(true);
		question.setPosition(1);
		question.setTitle("simple question");
		question.setType(QuestionType.FILE);

		manager.createQuestion(question);

		Assert.assertNotNull(question.getId());

		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		question = em.merge(question);
		em.remove(question);
		em.getTransaction().commit();
		em.close();
	}

	@Test
	public void testCreateQuestionWithProperties() {
		Question question = new Question();
		question.setObligatory(true);
		question.setPosition(1);
		question.setTitle("question with properties");
		question.setType(QuestionType.SIMPLECHOICE);

		List<Properties> properties = new LinkedList<Properties>();

		Properties p0 = new Properties();
		p0.setValue("property 0");

		properties.add(p0);

		Properties p1 = new Properties();
		p1.setValue("property 1");

		properties.add(p1);

		question.setProperties(properties);

		manager.createQuestion(question);

		Assert.assertNotNull(question.getId());

		for (Properties property : properties)
			Assert.assertNotNull(property.getId());

		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		question = em.merge(question);
		em.remove(question);
		em.getTransaction().commit();
		em.close();
	}
	
	@Test
	public void testDeleteQuestion() {
		Question question = new Question();
		question.setObligatory(true);
		question.setPosition(1);	
		question.setTitle("simple question");
		question.setType(QuestionType.FILE);

		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(question);
		em.getTransaction().commit();
		em.close();
		
		manager.deleteQuestion(question.getId());
		
		em = jpautils.createEntityManger();
		question = em.find(Question.class, question.getId());
		Assert.assertNull(question);
		
		em.close();
	}
	
	@Test
	public void testDeleteQuestionWithProperties() {
		Question question = new Question();
		question.setObligatory(true);
		question.setPosition(1);
		question.setTitle("question with properties");
		question.setType(QuestionType.SIMPLECHOICE);

		List<Properties> properties = new LinkedList<Properties>();

		Properties p0 = new Properties();
		p0.setValue("testDeleteQuestionWithProperties property 0");
		p0.setQuestion(question);
		properties.add(p0);

		Properties p1 = new Properties();
		p1.setValue("testDeleteQuestionWithProperties property 1");
		p1.setQuestion(question);
		properties.add(p1);

		question.setProperties(properties);

		manager.createQuestion(question);
		
		manager.deleteQuestion(question.getId());
		
		EntityManager em = jpautils.createEntityManger();
		question = em.find(Question.class, question.getId());
		Assert.assertNull(question);
		Properties property = em.find(Properties.class, p0.getId());
		Assert.assertNull(property);
		property = em.find(Properties.class, p1.getId());
		Assert.assertNull(property);
		em.close();
	}
	
	@Test
	public void testDeleteQuestionWithAnswers() {
		Question question = new Question();
		List<Answer> answers = new LinkedList<Answer>();
		
		Answer answer = new Answer();
		answer.setQuestion(question);
		answers.add(answer);
		
		EntityManager em = jpautils.createEntityManger();
		em.getTransaction().begin();
		em.persist(question);
		em.getTransaction().commit();
		em.close();
		
		manager.deleteQuestion(question.getId());
		
		em = jpautils.createEntityManger();
		question = em.find(Question.class, question.getId());
		Assert.assertNull(question);
		
		answer = em.find(Answer.class, answer.getId());
		Assert.assertNull(answer);
	}
}
